<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>百度地图05</title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
<link href="" rel="stylesheet">

<style type="text/css">  
    html{height:100%}  
    body{height:100%;margin:0px;padding:0px}  
    #container{height:100%}  
</style>  

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=7DDU0BmgurkEgCqiGyvAf55qAMKzMfv8"></script>

</head>
<body>
    <div id="container"></div> 

	<script type="text/javascript">

		alert("根据坐标数组自动获取地图中心点和缩放【编写代码实现】");

		//数据准备
		var points = [
		{"lng":116,"lat":40,"status":1,"id":50},
		{"lng":117,"lat":31,"status":1,"id":2},
		{"lng":116,"lat":34,"status":0,"id":3},
		{"lng":118,"lat":39,"status":0,"id":4},
		{"lng":110,"lat":35,"status":1,"id":5},

		{"lng":106.552113,"lat":29.555927,"status":1,"id":5},
		{"lng":105.726678,"lat":29.71462},

		// {"lng":100.012332,"lat":39.574658},
		// {"lng":40.82609,"lat":38.887844},
		// {"lng":160.482109,"lat":65.462587},
		// {"lng":104.296764,"lat":34.869358},
		// {"lng":91.786602,"lat":31.276714},

		];

		// var points = [];  // 没有坐标的情况【显示整个中国】
		
		var map = new BMap.Map("container");//初始化地图

		setZoom(points);//设置中心点和缩放级别。

		addMarker(points);//把原始数据的轨迹点添加到地图上。

		map.enableScrollWheelZoom();//滚轮放大缩小

		//根据原始数据计算中心坐标和缩放级别，并为地图设置中心坐标和缩放级别。
		function setZoom(points){
			if(points.length>0){
				var maxLng = points[0].lng;
				var minLng = points[0].lng;
				var maxLat = points[0].lat;
				var minLat = points[0].lat;

				// 循环获取极值
				var res;
				for (var i = points.length - 1; i >= 0; i--) {
					res = points[i];
					if(res.lng > maxLng) maxLng =res.lng;
					if(res.lng < minLng) minLng =res.lng;
					if(res.lat > maxLat) maxLat =res.lat;
					if(res.lat < minLat) minLat =res.lat;
				};

				// 根据极值确定中心点坐标
				var cenLng =(parseFloat(maxLng)+parseFloat(minLng))/2;
				var cenLat = (parseFloat(maxLat)+parseFloat(minLat))/2;

				// 根据极值获取缩放级别
				var zoom = getZoom(maxLng, minLng, maxLat, minLat);

				// 设置中心坐标和缩放级别
				map.centerAndZoom(new BMap.Point(cenLng,cenLat), zoom);  
			}else{
				//没有坐标，显示全中国。
				map.centerAndZoom(new BMap.Point(103.388611,35.563611), 5);  
			} 
		}

		//根据经纬极值计算绽放级别。本例核心代码。
		function getZoom (maxLng, minLng, maxLat, minLat) {
			var zoom = ["50","100","200","500","1000","2000","5000","10000","20000","25000","50000","100000","200000","500000","1000000","2000000"]//级别18到3。
			var pointA = new BMap.Point(maxLng,maxLat);  // 创建点坐标A
			var pointB = new BMap.Point(minLng,minLat);  // 创建点坐标B
			var distance = map.getDistance(pointA,pointB).toFixed(1);  //获取两点距离,保留小数点后1位
			for (var i = 0,zoomLen = zoom.length; i < zoomLen; i++) {
				if(zoom[i] - distance > 0){
					return 18-i+3;//之所以会多3，是因为地图范围常常是比例尺距离的10倍以上。所以级别会增加3。
				}
			};
		}

		//在轨迹点上创建图标，并添加点击事件
		function addMarker(points){ 
			var point,marker;
			// 创建标注对象并添加到地图   
			for(var i = 0,pointsLen = points.length;i <pointsLen;i++){
				point = new BMap.Point(points[i].lng,points[i].lat);	
				marker = new BMap.Marker(point);   
				map.addOverlay(marker);
			}
		}     

	</script>

</body>
</html>